package com.ctshk.rpc.finance.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.rpc.finance.dto.paidin.ChargeUsedRecordDTO;
import com.ctshk.rpc.finance.dto.paidin.PaidInListDTO;
import com.ctshk.rpc.finance.entity.FinancePaidIn;
import com.ctshk.rpc.finance.req.paidin.PaidInListReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 财务实付 Mapper 接口
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-04-12
 */
public interface FinancePaidInMapper extends BaseMapper<FinancePaidIn> {

    /**
     * 列表
     * @param req
     * @param iPage
     * @return
     */
    @Select("<script>SELECT a.id,a.pay_number,a.supplier_name,a.use_advance_charge,a.pay_status,a.approval_status,a.gmt_create,a.create_id,a.gmt_create,\n" +
            "b.payable_amount,b.paid_amount,b.unpay_amount,b.pay_amount,b.currency_name,b.pay_type\n" +
            "FROM t_finance_paid_in a\n" +
            "LEFT JOIN t_finance_paid_in_detail b ON a.id = b.paid_in_id\n" +
            "WHERE a.is_deleted = 0\n" +
            "<if test=\"req.supplierId != null\">AND a.supplier_id = #{req.supplierId}</if> \n" +
            "<if test=\"req.createTimeBegin != null and req.createTimeEnd != null\">AND a.collection_time BETWEEN #{req.createTimeBegin} AND #{req.createTimeEnd}</if> \n" +
            "<if test=\"req.payStatus != null\">AND a.pay_status = #{req.payStatus}</if> \n" +
            "<if test=\"req.approvalStatus != null\">AND a.approval_status = #{req.approvalStatus}</if> \n" +
            "<if test=\"req.payNumber != null and req.payNumber != ''\">AND a.pay_number LIKE CONCAT('%',#{req.payNumber},'%') </if> \n" +
            "<if test=\"req.useAdvanceCharge != null\">AND a.use_advance_charge = #{req.useAdvanceCharge}</if> \n" +
            "<if test=\"req.createId != null\">AND a.create_id = #{req.createId}</if> \n" +
            "<if test=\"req.currencyId != null\">AND b.currency_id = #{req.currencyId}</if> \n" +
            "ORDER BY a.gmt_create DESC</script>")
    Page<PaidInListDTO> queryListByCondition(@Param("req") PaidInListReq req,@Param("iPage") Page<PaidInListDTO> iPage);

    /**
     * 财务预付款使用记录
     * @param supplierId
     * @return
     */
    @Select("SELECT a.order_number,a.pay_status,a.order_type,a.currency_id,a.gmt_create,a.pay_amount,a.currency_name \n" +
            "FROM t_finance_paid_in_detail a\n" +
            "LEFT JOIN t_finance_paid_in b ON a.paid_in_id = b.id\n" +
            "WHERE b.supplier_id = #{supplier_id} AND b.is_deleted = 0 AND b.use_advance_charge = 1 ORDER BY a.gmt_create DESC")
    List<ChargeUsedRecordDTO> queryListBySupplier(@Param("supplier_id") Long supplierId);
}
